#pragma once

#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include "InetAddr.hpp"


using namespace std;

class Reactor;

class Connection
{
public:
  Connection()
      : _sockfd(-1),
        _events(0) {}

  void SetSockfd(int sockfd)
  {
    _sockfd = sockfd;
  }

  void SetEvents(uint32_t events)
  {
    _events = events;
  }

  uint32_t GetEvents()
  {
    return _events;
  }

  Reactor* GetOwner()
  {
    return _owner;
  }

  void setOwner(Reactor *owner)
  {
    _owner = owner;
  }

  bool IsOutBufferEmpty() { return _outbuffer.empty(); } 
  void DisCardOutString(int n)
  {
    _outbuffer.erase(0, n);
  }
  string &outstring()
  {
    return _outbuffer;
  }

  string &instring()
  {
    return _inbuffer;
  }

  int Sockfd()
  {
    return _sockfd;
  }
  void Append_in(const string &in)
  {
    _inbuffer + in;
  }
  void Append_Out(const string &out)
  {
    _outbuffer + out;
  }

  string& InBuffer()
  {
    return _inbuffer;
  }

  virtual void Sender() = 0;
  virtual void Recver() = 0;
  virtual void Excepter() = 0;


  void Close()
  {
    if(_sockfd>0)
    {
      close(_sockfd);
    }
  }

protected:
  int _sockfd;
  // 关系那一个客户端的读取和写操作
  string _inbuffer;
  string _outbuffer;
  InetAddr _peer_addr;

  uint32_t _events;

  Reactor *_owner;

  uint64_t _timestamp;
};